<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
    <title>checkin</title>
</head>
<body>
<?php 
if($_POST){
        
	$url = 'http://maps.googleapis.com/maps/api/geocode/json?address='.$_POST['lat'].','.$_POST['long'].'&sensor=false';
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $geoloc = curl_exec($ch);
 		
		$array_dec = json_decode($geoloc);
		/*echo "<pre>";		
        print_r($array_dec);
		echo "</pre>";
		*/
		$array = '';
		foreach($array_dec as $key){
			
			if($array == ''){
			$array = array(
				"casa" => $key[0]->address_components[0]->long_name,
				"rua" => $key[0]->address_components[1]->long_name,
				"bairro" => utf8_decode(($key[0]->address_components[2]->long_name)),
				"cidade" => $key[0]->address_components[3]->long_name,
				"estado" => $key[0]->address_components[4]->long_name,
				"uf" => $key[0]->address_components[4]->short_name,
				"pais" => $key[0]->address_components[5]->long_name,
				"paissigla" => $key[0]->address_components[5]->short_name,
				"cep" => $key[0]->address_components[6]->short_name,
				"lat" => $_POST['lat'],
				"long" => $_POST['long']
				
			);			
			}
			/*		
			echo "intervalo casas: ".($key[0]->address_components[0]->long_name)."<br />";//intervalo de casas
			echo "Rua: ".($key[0]->address_components[1]->long_name)."<br />";// rua
			echo "Bairro: ".utf8_decode(($key[0]->address_components[2]->long_name))."<br />";//bairro
			echo "Cidade: ".($key[0]->address_components[3]->long_name)."<br />";//cidade
			echo "Estado: ".($key[0]->address_components[4]->long_name)."<br />";//estado
			echo "UF: ".($key[0]->address_components[4]->short_name)."<br />";//estado sigla
			echo "Pais: ".($key[0]->address_components[5]->long_name)."/"//país
			.($key[0]->address_components[5]->short_name)."<br />";//país sigla
			echo "CEP: ".($key[0]->address_components[6]->short_name)."<br />";//CEP
			*/
		
		}


		
		echo "intervalo casas: ".$array["casa"]."<br />";//intervalo de casas
		echo "Rua: ".$array["rua"]."<br />";// rua
		echo "Bairro: ".$array["bairro"]."<br />";//bairro
		echo "Cidade: ".$array["cidade"]."<br />";//cidade
		echo "Estado: ".$array["estado"]."<br />";//estado
		echo "UF: ".$array["uf"]."<br />";//estado sigla
		echo "Pais: ".$array["pais"]."/"//país
		.$array["paissigla"]."<br />";//país sigla
		echo "CEP: ".$array["cep"]."<br />";//CEP
		echo "Latitude: ".$array["lat"]."<br />";//CEP
		echo "Longitude: ".$array["long"]."<br />";//CEP
?>
<section id="wrapper">

<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=true"></script>
    <article>
    </article>
<script>
function success(position) {
  var mapcanvas = document.createElement('div');
  mapcanvas.id = 'mapcontainer';
  mapcanvas.style.height = '400px';
  mapcanvas.style.width = '600px';

  document.querySelector('article').appendChild(mapcanvas);

 
  var coords = new google.maps.LatLng(<?php echo $_POST['lat']; ?>, <?php echo $_POST['long']; ?>);
  
  var options = {
    zoom: 15,
    center: coords,
    mapTypeControl: false,
    navigationControlOptions: {
    	style: google.maps.NavigationControlStyle.SMALL
    },
    mapTypeId: google.maps.MapTypeId.ROADMAP
  };
  var map = new google.maps.Map(document.getElementById("mapcontainer"), options);

  /*var marker = new google.maps.Marker({
      position: coords,
      map: map,
	  icon:"icone.png",
      title:"primeiro"
  });*/
  var marker = new google.maps.Marker({
      position: coords,
	  icon:"icone.png",
      map: map,
      title:"segundo"
  });
  
	var lat = position.coords.latitude;
	var long = position.coords.longitude;
	
	
	document.getElementById('lat').value=lat;
	document.getElementById('long').value=long;
	
}

if (navigator.geolocation) {
  navigator.geolocation.getCurrentPosition(success);
} else {
  error('Geo Location is not supported');
}
</script>
</section>

<?php } ?>




</body>
</html>